package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.MyCircleMultiListAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.MyCircle;
import com.isunland.managesystem.entity.MyCircleOriginal;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:  圈子复选列表
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/3/21 13:33
 */
public class MyCircleMultiListFragment extends BaseListFragment {
  protected static final String EXTRA_VALUE =
      "com.isunland.managesystem.ui.MyCircleMultiListFragment.EXTRA_VALUE";

  private ArrayList<MyCircle> mList;
  private MyCircleMultiListAdapter mAdapter;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.myCircle);
    mList = new ArrayList<>();
  }

  @Override public String getUrl() {
    return ApiConst.URL_MY_CIRCLE_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("jobno", CurrentUser.newInstance(getActivity()).getJobNumber());
    return params;
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        return true;
      case R.id.menu_item_confirm:
        //添加好友
        if (mList != null || mList.size() != 0) {
          StringBuilder bufferJob = new StringBuilder();
          StringBuilder bufferName = new StringBuilder();
          for (int i = mList.size() - 1; i >= 0; i--) {
            MyCircle item0 = mList.get(i);
            if (item0 == null) continue;
            String id = item0.getId();
            String name = item0.getCirclename();
            if (mAdapter.isCheckedItem(id)) {
              if (bufferJob.length() > 0) {
                bufferJob.append(",").append(id);
                bufferName.append(",").append(name);
              } else {
                bufferJob.append(id);
                bufferName.append(name);
              }
            }
          }

          if (bufferJob.length() == 0) {
            ToastUtil.show(R.string.choosePerson);
            return false;
          }
          CustomerDialog content = new CustomerDialog(bufferName.toString(), bufferJob.toString());
          Intent i = new Intent();
          i.putExtra(EXTRA_VALUE, content);
          LogUtil.i("jobnos===" + bufferJob.toString());
          getActivity().setResult(Activity.RESULT_OK, i);
          getActivity().finish();
        }
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public void resolveResponse(String json) {
    MyCircleOriginal original = new Gson().fromJson(json, MyCircleOriginal.class);
    if (original.getResult() == Base.SUCCESS && original.getRows() != null) {
      //获取列表成功
      mList.clear();
      mList.addAll(original.getRows());
      //            mAdapter = new MyFriendMultiListAdapter(getActivity(), mList);
      mAdapter = new MyCircleMultiListAdapter(getActivity(), mList);
      setListAdapter(mAdapter);
    } else {
      ToastUtil.show("获取列表失败");
    }
  }
}

